/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.guobo.dao.AssetDao;
import com.thinkgem.jeesite.modules.guobo.dao.ExhHallDao;
import com.thinkgem.jeesite.modules.guobo.dao.TrailLogDao;
import com.thinkgem.jeesite.modules.guobo.entity.Asset;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.ExhOutIn;
import com.thinkgem.jeesite.modules.guobo.entity.ExhOutInReport;
import com.thinkgem.jeesite.modules.guobo.entity.TrailLog;

/**
 * 轨迹Service
 * 
 * @author wudavid
 * @version 2017-03-09
 */
@Service
@Transactional(readOnly = true)
public class TrailLogService extends CrudService<TrailLogDao, TrailLog> {

	@Autowired
	private AssetDao assetDao;

	@Autowired
	private ExhHallDao exhHallDao;
	
	public TrailLog get(String id) {
		return super.get(id);
	}

	public List<TrailLog> findList(TrailLog trailLog) {
		return super.findList(trailLog);
	}

	public Page<TrailLog> findPage(Page<TrailLog> page, TrailLog trailLog) {
		return super.findPage(page, trailLog);
	}

	@Transactional(readOnly = false)
	public void save(TrailLog trailLog) {
		super.save(trailLog);
	}

	@Transactional(readOnly = false)
	public void delete(TrailLog trailLog) {
		super.delete(trailLog);
	}

	public List<TrailLog> searchNotPorcLogsByLoginUser(String userid) {
		return dao.searchNotPorcLogsByLoginUser(userid);
	}

	public Page<ExhOutInReport> getExhOutInReport(Page<ExhOutInReport> page, ExhOutInReport exhOutInReport,
			Map<String, Object> paramMap) {
		exhOutInReport.setPage(page);
		List<ExhOutInReport> result = dao.getExhOutInReport(paramMap);
		page.setList(result);
		page.setCount(result.size());
		return page;
	}

	public Page<TrailLog> findProcedLogPage(Page<TrailLog> page, TrailLog trailLog) {
		trailLog.setPage(page);
		page.setList(dao.findProcedList(trailLog));
		return page;
	}

	public Page<TrailLog> findLoseListPage(Page<TrailLog> page, TrailLog trailLog) {
		trailLog.setPage(page);
		page.setList(dao.findLoseList(trailLog));
		return page;
	}

	/**
	 * 统计非固定资产的出入库
	 * 
	 * @param page
	 * @param exhOutInReport
	 * @param paramMap
	 * @return
	 */
	public Page<ExhOutInReport> getExhOutInReportByParam(Page<ExhOutInReport> page, ExhOutInReport exhOutInReport,
			Map<String, Object> paramMap) {
		List<ExhOutInReport> result = new ArrayList<ExhOutInReport>();
		Asset asset = new Asset();
		TrailLog trailLog = new TrailLog();
		asset.setAssetType("1");
		trailLog.setAsset(asset);
		//List<ExhOutIn> outLogs = dao.findOutReport(trailLog);
		List<ExhOutIn> outLogs = dao.findOutReportByCondition(paramMap);
		Map<String, Integer> outs = new HashMap<String, Integer>();
		//List<ExhOutIn> inLogs = dao.findInReport(trailLog);
		List<ExhOutIn> inLogs = dao.findInReportByCondition(paramMap);
		Map<String, Integer> ints = new HashMap<String, Integer>();
		String exhIds = paramMap.get("exhIds") + "";
		for (ExhOutIn out : outLogs) {
			outs.put(out.getCode() + "-" + out.getExhHall().getId(), out.getOutNum());
		}
		for (ExhOutIn in : inLogs) {
			ints.put(in.getCode() + "-" + in.getExhHall().getId(), in.getInNum());
		}
		List<Asset> assets = assetDao.findAsstsMaterial(asset);
		String[] tempExhIds = exhIds.split(",");
		for (Asset as : assets) {
			ExhOutInReport er = new ExhOutInReport();
			String code = as.getCode();
			int outNum=0;
			int inNum=0;
			er.setAssetCode(as.getCode());
			er.setAssetModle(as.getModel());
			er.setAssetName(as.getName());
			er.setAssetSpec(as.getSpec());
			//er.setRegion(as.getExhHall().getRegion());
			//er.setExhHall(as.getExhHall());
			List<ExhOutIn> exhOuts = new ArrayList<ExhOutIn>();
			List<ExhOutIn> exhIns = new ArrayList<ExhOutIn>();
			for (String exhId : tempExhIds) {
				ExhOutIn exhOut = new ExhOutIn();
				ExhOutIn exhIn = new ExhOutIn();
				ExhHall exhHall = new ExhHall();
				exhHall.setId(exhId);
				ExhHall exh = exhHallDao.get(exhId);
				Integer out = outs.get(code + "-" + exhId);
				if (out == null) {
					out = 0;
				}
				Integer in = ints.get(code + "-" + exhId);
				if (in == null) {
					in = 0;
				}
				exhOut.setCode(code);
				exhOut.setOutNum(out);
				exhOut.setExhHall(exh);
				exhIn.setCode(code);
				exhIn.setInNum(in);
				inNum = inNum+in;
				outNum=outNum+out;
				exhIn.setExhHall(exh);
				exhOuts.add(exhOut);
				exhIns.add(exhIn);
			}
			Collections.sort(exhOuts, new Comparator<ExhOutIn>() {
				@Override
				public int compare(ExhOutIn o1, ExhOutIn o2) {
					// 按照code升序排序
					if (Integer.parseInt(o1.getExhHall().getCode()) > Integer.parseInt(o2.getExhHall().getCode())) {
						return 1;
					}
					if (Integer.parseInt(o1.getExhHall().getCode()) == Integer.parseInt(o2.getExhHall().getCode())) {
						return 0;
					}
					return -1;
				}
			});
			Collections.sort(exhIns, new Comparator<ExhOutIn>() {
				@Override
				public int compare(ExhOutIn o1, ExhOutIn o2) {
					// 按照code升序排序
					if (Integer.parseInt(o1.getExhHall().getCode()) > Integer.parseInt(o2.getExhHall().getCode())) {
						return 1;
					}
					if (Integer.parseInt(o1.getExhHall().getCode()) == Integer.parseInt(o2.getExhHall().getCode())) {
						return 0;
					}
					return -1;
				}
			});
			er.setExhIns(exhIns);
			er.setExhOuts(exhOuts);
			er.setTotalInNum(inNum+"");
			er.setTotalOutNum(outNum+"");
			result.add(er);
		}
		exhOutInReport.setPage(page);		
		page.setList(result);
		page.setCount(result.size());
		return page;
	}
	
	/**
	 * 统计固定资产的出入库
	 * 
	 * @param page
	 * @param exhOutInReport
	 * @param paramMap
	 * @return
	 */
	public Page<ExhOutInReport> getFixedExhOutInReportByParam(Page<ExhOutInReport> page, ExhOutInReport exhOutInReport,
			Map<String, Object> paramMap) {
		List<ExhOutInReport> result = new ArrayList<ExhOutInReport>();
		Asset asset = new Asset();
		TrailLog trailLog = new TrailLog();
		asset.setAssetType("0");
		trailLog.setAsset(asset);
		//List<ExhOutIn> outLogs = dao.findFixedOutReport(trailLog);
		List<ExhOutIn> outLogs = dao.findFixedOutReportByCondition(paramMap);
		Map<String, Integer> outs = new HashMap<String, Integer>();
		List<ExhOutIn> inLogs = dao.findFixedInReport(trailLog);
		Map<String, Integer> ints = new HashMap<String, Integer>();
		String exhIds = paramMap.get("exhIds") + "";
		for (ExhOutIn out : outLogs) {
			outs.put(out.getType() + "-" + out.getExhHall().getId(), out.getOutNum());
		}
		for (ExhOutIn in : inLogs) {
			ints.put(in.getType() + "-" + in.getExhHall().getId(), in.getInNum());
		}
		List<Asset> assets = assetDao.findFixedAssts(asset);
		String[] tempExhIds = exhIds.split(",");
		for (Asset as : assets) {
			ExhOutInReport er = new ExhOutInReport();
			String type = as.getType();
			er.setType(type);
			er.setTypeName(as.getTypeName());
			int outNum=0;
			int inNum=0;
			List<ExhOutIn> exhOuts = new ArrayList<ExhOutIn>();
			List<ExhOutIn> exhIns = new ArrayList<ExhOutIn>();
			for (String exhId : tempExhIds) {
				ExhOutIn exhOut = new ExhOutIn();
				ExhOutIn exhIn = new ExhOutIn();
				ExhHall exhHall = new ExhHall();
				exhHall.setId(exhId);
				ExhHall exh = exhHallDao.get(exhId);
				Integer out = outs.get(type + "-" + exhId);
				if (out == null) {
					out = 0;
				}
				Integer in = ints.get(type + "-" + exhId);
				if (in == null) {
					in = 0;
				}
				exhOut.setCode(type);
				exhOut.setOutNum(out);
				exhOut.setExhHall(exh);
				exhIn.setCode(type);
				exhIn.setInNum(in);
				inNum = inNum+in;
				outNum=outNum+out;
				exhIn.setExhHall(exh);
				exhOuts.add(exhOut);
				exhIns.add(exhIn);
			}
			Collections.sort(exhOuts, new Comparator<ExhOutIn>() {
				@Override
				public int compare(ExhOutIn o1, ExhOutIn o2) {
					// 按照code升序排序
					if (Integer.parseInt(o1.getExhHall().getCode()) > Integer.parseInt(o2.getExhHall().getCode())) {
						return 1;
					}
					if (Integer.parseInt(o1.getExhHall().getCode()) == Integer.parseInt(o2.getExhHall().getCode())) {
						return 0;
					}
					return -1;
				}
			});
			Collections.sort(exhIns, new Comparator<ExhOutIn>() {
				@Override
				public int compare(ExhOutIn o1, ExhOutIn o2) {
					// 按照code升序排序
					if (Integer.parseInt(o1.getExhHall().getCode()) > Integer.parseInt(o2.getExhHall().getCode())) {
						return 1;
					}
					if (Integer.parseInt(o1.getExhHall().getCode()) == Integer.parseInt(o2.getExhHall().getCode())) {
						return 0;
					}
					return -1;
				}
			});
			er.setExhIns(exhIns);
			er.setExhOuts(exhOuts);
			er.setTotalInNum(inNum+"");
			er.setTotalOutNum(outNum+"");
			result.add(er);
		}
		exhOutInReport.setPage(page);		
		page.setList(result);
		page.setCount(result.size());
		return page;
	}
}